package benny_driving_ser.util;
/**
 * 注:此方法涉及奖金等使用情况,不建议频繁使用
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import benny_driving_ser.ObjectBean.BonusRuleBean;

public class BonusUtil {
	
	public static int insertData(int total,int firstCost,int secondCost,int thirdCost,int fourthCost,int fifthCost){
		
		String sql_insert = "insert into t_bonus_rule1(id,bonus_cost) values(?,?);";
		Connection conn = DBUtil.getInstance().openConnection();
		try{
			conn.setAutoCommit(false);
			for(int i=0;i<total;i++){//1
				if(i==0){
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, firstCost);
					pst.executeUpdate();
					pst.close();
				}else if(0<i && i<=2){//2
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, secondCost);
					pst.executeUpdate();
					pst.close();
				}else if(2<i && i<=7){//3
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, thirdCost);
					pst.executeUpdate();
					pst.close();
				}else if(7<i && i<=17){//4
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, fourthCost);
					pst.executeUpdate();
					pst.close();
				}else if(17<i && i<=67){//5
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, fifthCost);
					pst.executeUpdate();
					pst.close();
				}else{//0
					PreparedStatement pst = conn.prepareStatement(sql_insert);
					pst.setInt(1, i+1);
					pst.setInt(2, 0);
					pst.executeUpdate();
					pst.close();
				}
			}
			conn.commit();
			conn.close();
			return 0;
		}catch(SQLException e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return 1;
		
	}
	
	
	public static void main(String[] args) {
		System.out.println(insertData(200, 100, 50, 20, 10, 5));
		
//		int totalBonus = 200;
//		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
//		map.put(100, 1);
//		map.put(50, 2);
//		map.put(20, 10);
//		
//		BonusRuleBean bonus = new BonusRuleBean();
//		bonus.setTotalBonusRules(20);
//		bonus.setTotalOrders(totalBonus);
//		bonus.setMap(map);
//		
//		createBonusRuleTable(bonus);
	}

}
